package iosr11.Service;

import iosr11.data.Event;
import iosr11.data.EventKind;
import iosr11.data.EventSeries;
import iosr11.data.Student;
import iosr11.data.StudentEventSeries;
import iosr11.data.Teacher;
import iosr11.gbase.dao.EventDAO;
import iosr11.gbase.dao.EventSeriesDAO;
import iosr11.gbase.dao.StudentEventSeriesDAO;
import iosr11.gbase.dao.TeacherDAO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class EventSeriesServiceImpl implements EventSeriesService{

	private EventSeriesDAO eventSeriesDAO;
	private EventDAO eventDAO;
	private StudentEventSeriesDAO studentEventSeriesDAO;
	private TeacherDAO teacherDAO;
	private Logger log = Logger.getLogger("service-log");
	
	public EventSeriesServiceImpl(){
		
	}
	
	
	
	@Override
	public Class<EventSeries> getServiceEntityClass() {
		return EventSeries.class;
	}



	@Override
	public EventSeries getEventSeries(String id) {
		log.info("pobieranie serii po id");
		EventSeries series = eventSeriesDAO.getByID(id);
		return series;
	}

	@Override
	public boolean createEventSeries(EventKind kind, Teacher teacher,
			List<Event> events) {
		EventSeries series = new EventSeries();
		series.setKind(kind);
		series.setEvents(events);
		series.setTeacher(teacher);
		return this.addEventSeries(series);
	}

	@Override
	public boolean addEventSeries(EventSeries eventSeries) {
		Logger log = Logger.getLogger("service-log");
		log.info("addEventSeries");
		return eventSeriesDAO.add(eventSeries);
	}

	@Override
	public boolean deleteEventSeries(EventSeries eventSeries) {
		List<Event> events = eventSeries.getEvents();
		if (events == null){
			log.info("brak eventow do serii - null");
		}
		else if (events.size()<1){
			log.info("brak eventow dla serii - ilosc 0");
		}
		else {
			log.info("do serii przypiane sa eventy - usuwanie");
			for(Event event : events){
				if (!eventDAO.delete(event)){
					log.warning("nie mozna usunac eventu do event serii");
				}
			}
		}
		List<StudentEventSeries> studentsForSeries = studentEventSeriesDAO.getStudentsForEventSeries(eventSeries);
		if (studentsForSeries == null){
			log.info("brak studentow przypisanych do event serii - null");
		}
		else if (studentsForSeries.size()<1){
			log.info("brak studentow przypisanych do event serii - size < 1");
		}
		else {
			log.info("znaleziono studentow przypisanych do event serii - usuwanie");
			for (StudentEventSeries ses : studentsForSeries){
				studentEventSeriesDAO.delete(ses);
			}
		}
		if (eventSeriesDAO.delete(eventSeries)){
			log.info("pomyslnie usunieto seriie eventow");
			return true;
		}
		else {
			log.info("nie udalo sie usunac serii eventow");
			return false;
		}
	}

	@Override
	public boolean removeTeacherFromEventSeries(Teacher teacher,
			EventSeries series) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean setTeacherToEventSeries(Teacher teacher, EventSeries series) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateEventSeries(EventSeries eventSeries) {
		log.info("aktualizacja serii eventow - tylko dane o serii");
		return eventSeriesDAO.update(eventSeries);
	}

	@Override
	public List<EventSeries> getAllEventSeries() {
		log.info("wyswietlenie wszystkich serii zapisanych w bazie");
		List<EventSeries> lista =  eventSeriesDAO.getAll();
		if (lista==null){
			return new ArrayList<EventSeries>();
		}
		else
			return lista;
	}

	@Override
	public List<EventSeries> getStudentEventSeries(Student student) {
		log.info("getEvetSeriesForStudent student-name: " + student.getName());
		
		List <EventSeries> result = new ArrayList<EventSeries>();
		List<StudentEventSeries> tmp = studentEventSeriesDAO.getEventSeriesForStudent(student);
		if (tmp!=null){
			for (StudentEventSeries se : tmp){
				EventSeries series = eventSeriesDAO.getByID(se.getSeries().getId());
				if (series.getTeacher()!=null){
				// dodanie danyc o nauczycielu
				
					series.setTeacher(teacherDAO.getByID(series.getTeacher().getId()));
				}
				result.add(series);
			}
		}
		/*funkcja zwraca liste serii lecz bez poszczegolnych eventow*/
		return result;
	}

	@Override
	public boolean addStudentToEvents(Student student, EventSeries series) {
		StudentEventSeries tmp = new StudentEventSeries(student,series);
		return studentEventSeriesDAO.add(tmp);
	}

	@Override
	public boolean addEventToSeries(Event event, EventSeries series) {
		log.info("dodanie eventu do serii eventow");
		event.setSeries(series);
		return eventDAO.update(event);
	}

	@Override
	public boolean removeEventFromSeries(Event event, EventSeries series) {
		/* funkcja analogiczna jak usuwanie eventu*/
		log.info("usuwanie eventu");
		if (series.getEvents()!=null)
			series.getEvents().remove(event);
		return eventDAO.delete(event);
	}

	@Override
	public boolean removeStudentFromSeries(Student student, EventSeries series) {
		List<StudentEventSeries> studentSeries =  studentEventSeriesDAO.getEventSeriesForStudent(student);
		for (StudentEventSeries tmp : studentSeries){
			if (tmp.getSeries().getId().equals(series.getId()) ){
				log.info("znaleiono polaczenie - usuwanie");
				return studentEventSeriesDAO.delete(tmp);
			}
		}
		log.info("nie znaleziono polacenia - nic nie usunieto");
		return false;
	}


	
	// ==============================================================
	// settery dla springa
	

	public void setEventSeriesDAO(EventSeriesDAO eventSeriesDAO) {
		this.eventSeriesDAO = eventSeriesDAO;
	}

	public void setStudentEventSeriesDAO(StudentEventSeriesDAO studentEventSeriesDAO) {
		this.studentEventSeriesDAO = studentEventSeriesDAO;
	}

	public void setTeacherDAO(TeacherDAO teacherDAO) {
		this.teacherDAO = teacherDAO;
	}

	public void setEventDAO(EventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}
	

}
